# Maintainer: Christian Rebischke <chris.rebischke@archlinux.org>
# Maintainer: Daurnimator <daurnimator@archlinux.org>
# Maintainer: Justin Kromlinger <hashworks@archlinux.org>
# Contributor: Jerome Leclanche <jerome@leclan.ch>

pkgname=terraform
pkgver=1.6.4
pkgrel=1
pkgdesc="HashiCorp tool for building and updating infrastructure as code idempotently"
url="https://github.com/hashicorp/terraform"
arch=("loong64" "x86_64")
license=("BUSL-1.1")
makedepends=('go')
depends=('glibc')
optdepends=('diffutils: for running `terraform fmt -diff`')
options=('!lto')
source=("$pkgname-$pkgver.tar.gz::https://github.com/hashicorp/terraform/archive/v$pkgver.tar.gz"
        "terraform.bash"
        "terraform.zsh")
sha512sums=('7564824d301bd99786b9248eeab950141b3861f1c5ce51bce45b55c7a9ba1d66409a2485a717a100f01ce4d3eb718a299fe8d048661cf88101aef43364446b91'
            'ea42bfc41288e5ad741fcddfbd5c1f8ba0088be0967b820db1e12e1dc13d1ba242b9cc1c247bf29c36ec59cdce76ff6703cf6b3f76cca5ec441a4927a01afe7d'
            'ad991daf40f68c11fa66256177a04d97bd38d6a89c554d6261489d4de703852628ffc37429d862237ca24e15f6a7c915cfa027731189ac89a27f7b20c72ab4c9')
b2sums=('b68b7757d19148849bc0b976d2709034112240bd0098205d23d979cde2e686e02d1bc2b48d5b938dadd83afc83310ec14edbfd307735eb2326204e6ce5d58774'
        'd047735bbb006e9afbaa4b18af7a7f16a205ec528e91caa61a0b663dd6e24ec1df999216f0b7bd06e3b3c087b37f6ce3aa3cf3a20c892a9cc1962d33ce1b0bcc'
        'd58712c8203e4a58ec68738f3d22457547a55d230018408400fd1ca433346171b72fa4a87f05efc93213c71f9757d3a8072149ca4e6894355e79f8cfae3bd55b')

prepare() {
	cd "$pkgname-$pkgver"
	go mod download
}

build() {
	cd "$pkgname-$pkgver"
	export CGO_CPPFLAGS="${CPPFLAGS}"
	export CGO_CFLAGS="${CFLAGS}"
	export CGO_CXXFLAGS="${CXXFLAGS}"
	export CGO_LDFLAGS="${LDFLAGS}"
	export GOFLAGS="-buildmode=pie -trimpath -mod=readonly -modcacherw '-ldflags=-linkmode=external \"-X=github.com/hashicorp/terraform/version.dev=no\"'"
	export GOPROXY=off
	go build -o terraform-binary
}

check() {
	cd "${pkgname}-${pkgver}"
	go test -mod=readonly -skip TestInit_cancelModules -skip TestMTLSServer_NoCertFails ./...
}

package() {
	install -Dm644 terraform.bash "$pkgdir/usr/share/bash-completion/completions/terraform"
	install -Dm644 terraform.zsh "$pkgdir/usr/share/zsh/site-functions/_terraform"

	cd "$pkgname-$pkgver"
	install -Dm755 terraform-binary "$pkgdir/usr/bin/terraform"
	install -Dm644 LICENSE "$pkgdir/usr/share/licenses/$pkgname/LICENSE"
}
